﻿using Core.Framework;
using iTool.ClusterComponent;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Silo.CloudService
{
    public class LifetimeEventsHostedService : IHostedService
    {
        private readonly IHostApplicationLifetime _appLifetime;

        public LifetimeEventsHostedService(IHostApplicationLifetime appLifetime)
        {
            _appLifetime = appLifetime;
        }



        public Task StartAsync(CancellationToken cancellationToken)
        {
            Print.Line("\tiTool> Plugins http hosting starting...", ConsoleColor.Cyan);

            _appLifetime.ApplicationStarted.Register(OnStarted);
            _appLifetime.ApplicationStopping.Register(OnStopping);
            _appLifetime.ApplicationStopped.Register(OnStopped);
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            Print.Debug("\niTool> Plugins http hosting stoping...");
            return Task.CompletedTask;
        }


        #region IHostApplicationLifetime

        private void OnStarted()
        {

            // Perform post-startup activities here
            Print.EndSuccess("\niTool> Start plugins http host handel success.\n");
        }

        private void OnStopping()
        {
            // Perform on-stopping activities here
        }

        private void OnStopped()
        {
            // Perform post-stopped activities here
        }

        #endregion

    }
}
